package com.etc.controller;

import com.etc.entity.Hospital;
import com.etc.entity.Notice;
import com.etc.mapper.HospitalMapper;
import com.etc.service.HospitalService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/hospital")
public class HospitalController {

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private HospitalService hospitalService;


    @RequestMapping("/hospital")
    public String HospitalView(Model model) {

        List<Hospital> hospital = hospitalService.queryAll();
        model.addAttribute("hospital", hospital);
        return "hospital";
    }

    @RequestMapping("/hospitalDelete")
    public String HospitalDelete(int id) {
        hospitalService.deleteById(id);
        return "redirect:/hospital/hospital/1";

    }

    @RequestMapping("/hospitalInsert")
    public String HospitalInsert(@Param("id") int id, @Param("name") String name, @Param("site") String site, @Param("area") int area) {
        hospitalService.insertHospital(id, name, site, area);
        return "redirect:/hospital/hospital/1";
    }


    @RequestMapping("/hospitalBy")
    public String HospitalBy(Model model, String text) {
        List<Hospital> list = hospitalService.queryBy(text);
        model.addAttribute("hospital", list);
        return "hospital";
    }

    @RequestMapping("/hospitalByAreas")
    public String HospitalByAreas(Model model) {
        List<Hospital> list = hospitalService.queryByArea(1);
        model.addAttribute("list",list);
        return "index";
    }

    @RequestMapping("/hospitalByArea")
    public String HospitalByArea(Hospital hospital,Model model) {
        List<Hospital> list = hospitalService.queryByArea(hospital.getHospital_area());
        model.addAttribute("list",list);
        return "index";
    }

    @RequestMapping("/hospitalById")
    @ResponseBody
    public Hospital HospitalById(@RequestBody Hospital hospital) {
        Hospital hospital1 = hospitalService.queryById(hospital.getHospital_id());
        return hospital1;
    }

    @RequestMapping("/hospitalUpdate")
    public String HospitalUpdate(Hospital hospital) {
        hospitalService.updateHospital(hospital);
        return "redirect:/hospital/hospital/1";
    }

    @RequestMapping(value = "/hospital/{pagenum}", method = RequestMethod.GET)
    public String rolesmanager(Model model, @PathVariable(value = "pagenum", required = false) String pagenum) {
        System.out.println("进入方法" + pagenum);
        //每页长度
        int pagesize = 5;
        //当前页
        int pagecurrent = 1;
        //查询数据库总数据
        int counttotal = hospitalService.queryAll().size();
        //判断总页数
        int pages = 1;
        if (counttotal / pagesize == 0) {
            pages = 1;
        } else {
            pages = counttotal / pagesize + 1;
        }
        //判断当前页
        if (pagenum == null) {
            pagecurrent = 1;
        } else {
            pagecurrent = Integer.valueOf(pagenum);
        }
        //首页每页判断
        if (pagecurrent <= 1) {
            pagecurrent = 1;
        } else if (pagecurrent >= pages) {
            pagecurrent = pages;
        }
        //分页start
        int pagestart = 0;
        pagestart = (pagecurrent - 1) * pagesize;


        //根据当前页和页面尺度进行分页
        PageHelper.startPage(pagecurrent, pagesize);
        List<Hospital> hospital = hospitalService.queryAll();
        System.out.println(hospital);
        //加入角色列表信息
        model.addAttribute("hospital", hospital);
        //加入总页数
        model.addAttribute("pages", pages);
        //加入当前页
        model.addAttribute("pagecurrent", pagecurrent);
        //加入查询总条数
        model.addAttribute("counttotal", counttotal);
        //加入每页宽度
        model.addAttribute("pagesize", pagesize);
        //返回页面
        return "hospital";
    }
}
